package controllers;

import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
import models.User;
import models.Course;

public class SubscribeCourseTeacherController extends HttpServlet {

    private int courseid, userid;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);
        HttpSession sessie = request.getSession();        
        if (request.getParameter("courseid") != null) {
            dbManager.closeConnection();
            courseid = Integer.parseInt(request.getParameter("courseid"));
            userid = Integer.parseInt(request.getParameter("userid"));
            doPost(request, response);
        } else if(request.getParameter("userid") != null){
            int userid = Integer.parseInt(request.getParameter("userid"));
            LinkedList<Course> allcourses = queryManager.getAllCourses();
            LinkedList<Course> teachercourses = queryManager.getAllTeacherCourses(userid);
            LinkedList<Course> subscribecourses = new LinkedList<Course>();
            Boolean allsubscribed;
            
            for(Course course : allcourses){
                boolean contains = false;
                for(Course tCourse : teachercourses){
                    if(course.getCourseID() == tCourse.getCourseID()){
                        contains = true;
                    }
                }
                if(contains==false){
                    subscribecourses.add(course);
                }
            }
            
            /*for (int i = 0; i < allcourses.size(); i++) {
                allsubscribed = false;
                
                for (int j = 0; j < teachercourses.size(); j++) {
                    Course tempcourse1 = allcourses.get(i);
                    Course tempcourse2 = teachercourses.get(j);
                    if (tempcourse1.getCourseID() == tempcourse2.getCourseID()) {
                        allsubscribed = true;
                    }
                }
                if (allsubscribed == false) {
                    subscribecourses.add(allcourses.get(i));
                }
            }*/

            request.setAttribute("subscribecourses", subscribecourses);
            request.setAttribute("userId", userid);

            dbManager.closeConnection();

            RequestDispatcher dispatcher = request.getRequestDispatcher("/subscribe_course_teacher.jsp");
            dispatcher.forward(request, response);            
        } else {
            LinkedList<User> gebruikersLijst;
            LinkedList<User> leraren = new LinkedList<User>();
            gebruikersLijst = queryManager.getallusers(); 
            for(User user : gebruikersLijst){
                if(user.getLevel()==2){
                    leraren.add(user);
                }
            }
            request.setAttribute("gebruikersLijst", leraren);
            dbManager.closeConnection();

            RequestDispatcher dispatcher = request.getRequestDispatcher("/subscribe_course_teacher.jsp");
            dispatcher.forward(request, response);                
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        Dbmanager dbManager = new Dbmanager();
        dbManager.openConnection();
        QueryManager queryManager = new QueryManager(dbManager);
        HttpSession sessie = request.getSession();

        User user = (User) sessie.getAttribute("gebruikerinfo");

        if (user.getLevel() > 1) {
            queryManager.addteachertocourse(userid, courseid);
        }

        dbManager.closeConnection();
        response.sendRedirect("subscribe_course_teacher");

    }
}
